package com.dqhl.community.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.dqhl.community.R;
import com.dqhl.community.model.Community;
import com.dqhl.community.util.ScreenUtils;
import com.dqhl.community.util.ViewHolder;

import java.util.List;


/**
 * Created by Administrator on 2016/8/2.
 * 选择楼宇、单元的 popupwindow
 */
public class BuildingSelectPopupWindow extends PopupWindow {

    private Activity context;
    private ListView lvName;

    private List<Community> list;

    public BuildingSelectPopupWindow(Activity context, List<Community> list, AdapterView.OnItemClickListener listener) {
        this.context = context;
        this.list = list;
        View view = LayoutInflater.from(context).inflate(R.layout.popupwindow_building_select, null);

        TextView tvFilterBg = (TextView) view.findViewById(R.id.tv_filter_bg);
        lvName = (ListView) view.findViewById(R.id.lv_name);
        MyAdapter adapter = new MyAdapter(context, list);
        lvName.setAdapter(adapter);
        lvName.setOnItemClickListener(listener);

        tvFilterBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        setBackgroundDrawable(new ColorDrawable(0x70000000));

        int width = (int) (ScreenUtils.getScreenWidth(context) * 0.7);
        setWidth(width);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setFocusable(true);
        setOutsideTouchable(true);
        setContentView(view);
        update();
    }

    class MyAdapter extends BaseAdapter {

        private Context context;
        private List<Community> list;

        public MyAdapter(Context context, List<Community> list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if (view == null) {
                view = LayoutInflater.from(context).inflate(R.layout.lv_item_building, viewGroup, false);
            }
            TextView tvName = ViewHolder.get(view, R.id.tv_name);
            tvName.setText(list.get(i).getName());
            return view;
        }
    }
    
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0+
        context.getWindow().setAttributes(lp);
    }


}
